package ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.fy.fyzf.R;
import com.fy.fyzf.base.BaseActivity;
import com.fy.fyzf.base.Contans;
import com.fy.fyzf.bean.MessageSendBean;
import com.fy.fyzf.bean.RegisterBean;
import com.fy.fyzf.bean.BaseData;
import com.fy.fyzf.presenter.RegisterPresenter;
import com.fy.fyzf.statusbar.Eyes;
import com.fy.fyzf.utils.AppUtils;
import com.fy.fyzf.utils.PhoneFormatCheckUtils;
import com.fy.fyzf.view.RegisterCallbaclk;

import butterknife.BindView;
import butterknife.OnClick;

public class RegisterActivity extends BaseActivity<RegisterPresenter> implements RegisterCallbaclk {
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.tv_login)
    TextView tvLogin;
    @BindView(R.id.ll_join)
    RelativeLayout llJoin;
    @BindView(R.id.tv_people)
    TextView tvPeople;
    @BindView(R.id.et_account)
    EditText etAccount;
    @BindView(R.id.et_company)
    EditText etCompany;
    @BindView(R.id.et_eamil)
    EditText etEamil;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.tv_yzm)
    TextView tvYzm;
    @BindView(R.id.et_yzm)
    EditText etYzm;
    @BindView(R.id.tv_time_down)
    TextView tvTimeDown;
    @BindView(R.id.tv_updata)
    TextView tvUpdata;
    private MyCount mc;

    @Override
    protected RegisterPresenter createPresenter() {
        return new RegisterPresenter(this);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_register;
    }

    @Override
    public void initView() {
        super.initView();
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(tvLogin.getText().toString());
        spannableStringBuilder.setSpan(new ForegroundColorSpan(Color.parseColor("#B3B2B2")), 0, 7, spannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableStringBuilder.setSpan(new ForegroundColorSpan(Color.parseColor("#457FFF")), 7, 9, spannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvLogin.setText(spannableStringBuilder);


        SpannableStringBuilder span = new SpannableStringBuilder(tvPeople.getText().toString());
        span.setSpan(new ForegroundColorSpan(Color.parseColor("#FF0000")), 0, 1, span.SPAN_EXCLUSIVE_EXCLUSIVE);
        span.setSpan(new ForegroundColorSpan(Color.parseColor("#3F3F3F")), 1, tvPeople.getText().toString().length(), span.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvPeople.setText(span);

        SpannableStringBuilder span2 = new SpannableStringBuilder(tvPhone.getText().toString());
        span2.setSpan(new ForegroundColorSpan(Color.parseColor("#FF0000")), 0, 1, span2.SPAN_EXCLUSIVE_EXCLUSIVE);
        span2.setSpan(new ForegroundColorSpan(Color.parseColor("#3F3F3F")), 1, tvPeople.getText().toString().length(), span2.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvPhone.setText(span2);
        mc = new MyCount(60 * 1000 * 3, 1000);
    }

    @Override
    public void initData() {
        super.initData();
        Eyes.translucentStatusBar(this);
    }

    private MessageSendBean messageSendBean = new MessageSendBean();
    private RegisterBean registerBean = new RegisterBean();
    @OnClick({R.id.tv_time_down, R.id.tv_updata, R.id.iv_close})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                finish();
                break;
            case R.id.tv_time_down:
                if (!canClick) return;
                Contans.REQUEST_TYPE = 1;
                if (!PhoneFormatCheckUtils.isChinaPhoneLegal(etPhone.getText().toString())) {
                    AppUtils.showToast("手机号不正确");
                    return;
                }
                messageSendBean.setIdentification(1);
                messageSendBean.setPhone(etPhone.getText().toString());
                mPresenter.getCode(messageSendBean);
                break;
            case R.id.tv_updata:
                if (TextUtils.isEmpty(etPhone.getText().toString())) {
                    AppUtils.showToast("请输入手机号");
                    return;
                }
                if (TextUtils.isEmpty(etAccount.getText().toString())) {
                    AppUtils.showToast("请输入姓名");
                    return;
                }
                if (TextUtils.isEmpty(etYzm.getText().toString())) {
                    AppUtils.showToast("请输入验证码");
                    return;
                }
                Contans.REQUEST_TYPE = 2;
                registerBean.setApplicant(etAccount.getText().toString());
                registerBean.setCompany(etCompany.getText().toString());
                registerBean.setEmail(etEamil.getText().toString());
                registerBean.setPhone(etPhone.getText().toString());
                registerBean.setVerify(etYzm.getText().toString());
                mPresenter.toRegister(registerBean);
                break;
        }
    }

    /*定义一个倒计时的内部类*/
    class MyCount extends CountDownTimer {
        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            handler.sendEmptyMessage(1);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            Message msg = Message.obtain();
            msg.what = 2;
            msg.obj = millisUntilFinished / 1000;
            handler.sendMessage(msg);
        }
    }
    private boolean canClick = true;//是否可以点击
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            boolean call = false;
            switch (msg.what) {
                case 1:
                    canClick = true;
                    call = true;
                    tvTimeDown.setText(R.string.get_code);
                    break;
                case 2:
                    tvTimeDown.setText(msg.obj.toString() + "s");
                    break;
                default:
                    break;

            }
            return call;
        }
    });
    @Override
    public void registetrSuccess(BaseData baseData) {
        startActivity(new Intent(this, ResigterResultActivity.class));
        finish();
    }

    @Override
    public void getCodeSuccess(BaseData baseData) {
        canClick = false;
        AppUtils.showToast("验证码发送成功");
        mc.start();
    }
}